package org.keycloak.examples.federation.properties;

import org.jboss.logging.Logger;
import org.keycloak.Config;
import org.keycloak.component.ComponentModel;
import org.keycloak.models.KeycloakSession;
import org.keycloak.storage.UserStorageProviderFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * MyUserStorageProviderFactory
 *
 * @return
 * @exception
 * @author : chenyao
 * @date : 2020/5/14
 */
public class MyUserStorageProviderFactory
        implements UserStorageProviderFactory<MyUserStorageProvider> {

    public static final String PROVIDER_NAME = "my-user";

    @Override
    public String getId() {
        return PROVIDER_NAME;
    }

    private static final Logger logger = Logger.getLogger(MyUserStorageProviderFactory.class);
    protected Properties properties = new Properties();

    @Override
    public void init(Config.Scope config) {
        /**
         * 我们可以指定用户属性文件的类路径，而不是对其进行硬编码。然后，您可以在中检索配置
         */
        String path = config.get("path");
        InputStream is = getClass().getClassLoader().getResourceAsStream(path);
        if (is == null) {
            logger.warn("Could not find users.properties in classpath");
        } else {
            try {
                properties.load(is);
            } catch (IOException ex) {
                logger.error("Failed to load users.properties file", ex);
            }
        }
    }

    @Override
    public MyUserStorageProvider create(KeycloakSession session, ComponentModel model) {
        return new MyUserStorageProvider(session, model, properties);
    }
}


